package com.bianguo.android.tencentpushdemo.view.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bianguo.android.tencentpushdemo.R;
import com.bianguo.android.tencentpushdemo.utils.MyLog;
import com.bianguo.android.tencentpushdemo.utils.RecorderHelper;
import com.bianguo.android.tencentpushdemo.utils.TimeFormatUtil;
import com.bianguo.android.tencentpushdemo.view.widget.SateliteMenu;

import butterknife.BindView;
import butterknife.OnClick;

public class RecordActivity extends BaseActivity implements View.OnTouchListener, RecorderHelper.OnAudioStatusUpdateListener, SateliteMenu.onMenuItemClickListener, AppBarLayout.OnOffsetChangedListener {

    @BindView(R.id.record_tv)
    TextView recordTv;
    @BindView(R.id.record_sign_layout)
    LinearLayout recordSignLayout;
    @BindView(R.id.record_time)
    TextView recordTimeTv;
    @BindView(R.id.record_image)
    ImageView recordIv;
    @BindView(R.id.menu_floating)
    FloatingActionButton menuFloating;
    @BindView(R.id.satelite_menu)
    SateliteMenu sateliteMenu;
    @BindView(R.id.record_toolbar)
    Toolbar toolbar;
    @BindView(R.id.collapsing_layout)
    CollapsingToolbarLayout collapsingLayout;
    @BindView(R.id.appBarLayout)
    AppBarLayout appBarLayout;

    private RecorderHelper recorderHelper;
    private String filePath;

    @Override
    protected void setNotiSystemWindows(int color) {
        super.setNotiSystemWindows(Color.TRANSPARENT);
    }

    @Override
    public int layoutId() {
        return R.layout.activity_record;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        setSupportActionBar(toolbar);
        collapsingLayout.setTitle("朋友圈");
        collapsingLayout.setCollapsedTitleGravity(Gravity.CENTER);
//        collapsingLayout.setCollapsedTitleTextColor(Color.TRANSPARENT);
        recordTv.setOnTouchListener(this);
        sateliteMenu.setOnMenuItemClickListener(this);
        appBarLayout.addOnOffsetChangedListener(this);
        new AlertDialog.Builder(mContext)
                .create();
    }

    @Override
    public void initData() {
        recorderHelper = new RecorderHelper();
        recorderHelper.setOnAudioStatusUpdateListener(this);
    }

    @OnClick({R.id.play_record_btn, R.id.menu_floating})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.play_record_btn:
                if (!TextUtils.isEmpty(filePath)) {
                    if (recorderHelper.isPlayRecording()) {
                        recorderHelper.stopRecording();
                    } else {
                        recorderHelper.playRecording(filePath);
                    }
                } else {
                    Toast.makeText(mContext, "没有录音文件", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.menu_floating:
                Snackbar.make(view, "floatingActionButton", Snackbar.LENGTH_LONG)
                        .setAction("撤销", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Toast.makeText(mContext, "撤销成功", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .show();
                break;
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                recordTv.setText("松开结束录音");
                recorderHelper.startRecord();
                recordSignLayout.setVisibility(View.VISIBLE);
                break;
            case MotionEvent.ACTION_UP:
                recordTv.setText("按住录音");
//                recorderHelper.stopRecord();
                recorderHelper.cancelRecord();
                recordSignLayout.setVisibility(View.GONE);
                break;
            case MotionEvent.ACTION_MOVE:
                float x = event.getX();
                float y = event.getY();
                float lastX = 0.0f;
                float lastY = 0.0f;
                MyLog.myLog("x = " + x + ", y = " + y);
                break;
        }
        return true;
    }

    @Override
    public void onUpdate(double db, long time) {
        //根据分贝值来设置录音时话筒图标的上下波动，下面有讲解
        int level = (int) (3000 + 6000 * db / 100);
        MyLog.myLog("db = " + db + ", level = " + level);
        recordIv.getDrawable().setLevel(level);
        recordTimeTv.setText(TimeFormatUtil.longToString(time, "mm:ss"));
    }

    @Override
    public void onStop(String filePath) {
        this.filePath = filePath;
        Toast.makeText(this, "录音保存在：" + filePath, Toast.LENGTH_SHORT).show();
        recordTimeTv.setText(TimeFormatUtil.longToString(0, "mm:ss"));
    }

    @Override
    public void onItemClick(View view, int position) {
        Toast.makeText(mContext, "position = " + position, Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_leftTop) {
            sateliteMenu.setPosition(SateliteMenu.Position.POS_LEFT_TOP);
            return true;
        }
        if (id == R.id.action_rightTop) {
            sateliteMenu.setPosition(SateliteMenu.Position.POS_RIGHT_TOP);
            return true;
        }
        if (id == R.id.action_leftBottom) {
            sateliteMenu.setPosition(SateliteMenu.Position.POS_LEFT_BOTTOM);
            return true;
        }
        if (id == R.id.action_rightBottom) {
            sateliteMenu.setPosition(SateliteMenu.Position.POS_RIGHT_BOTTOM);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        MyLog.myLog("verticalOffset = " + verticalOffset);
    }
}
